<template>
  <div class="login-container">
    <div class="login-box">
      <div class="login-header">
        <h2>第三方登录</h2>
        <p>选择常用账号快速登录</p>
      </div>
      <van-cell-group class="cell-group">
        <!-- 图片容器：点击后显示在按钮上方 -->
        <div v-if="activeImage" class="image-wrap">
          <img :src="activeImage" alt="登录图片" class="login-image" />
        </div>
        <!-- 登录按钮区域：默认垂直排列，点击后切换为水平排列 -->
        <div 
          :class="['btn-group', { 'horizontal-layout': isButtonLayoutHorizontal }]"
        >
          <van-button 
            round 
            block 
            :class="['login-btn', item.platform, { 
              'collapsed-btn': isButtonLayoutHorizontal && item.platform !== activePlatform 
            }]"
            v-for="(item, index) in thirdPartyList" 
            :key="index"
            @click="handleLogin(item)"
          >
            <i :class="item.icon"></i> 
            <span v-if="!isButtonLayoutHorizontal || item.platform === activePlatform">
              {{ item.text }}
            </span>
            <span v-if="isButtonLayoutHorizontal && item.platform !== activePlatform">
              {{ item.text.slice(0, 2) }}
            </span>
          </van-button>
        </div>
      </van-cell-group>
      <div class="other-login-options">
        <van-button @click="goToPasswordLogin" class="other-login-btn">
          密码登录
        </van-button>
        <van-button @click="onThirdLogin" class="other-login-btn">
          验证码登录
        </van-button>
      </div>
      <div class="goto">
        <van-button plain @click="onRegister" class="register-link">
          若还没有账号？点击前往注册
        </van-button>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  name: 'ThirdLogin',
  data() {
    return {
      thirdPartyList: [
        { 
          icon: 'iconfont icon-wechat', 
          text: '微信登录', 
          platform: 'wechat', 
          imageImport: () => import('@/assets/微信.png') 
        },
        { 
          icon: 'iconfont icon-alipay', 
          text: '支付宝登录', 
          platform: 'alipay', 
          imageImport: () => import('@/assets/支付宝.png') 
        },
        { 
          icon: 'iconfont icon-qq', 
          text: '企鹅登录', 
          platform: 'qq', 
          imageImport: () => import('@/assets/qq.png') 
        },
        { 
          icon: 'iconfont icon-weibo', 
          text: '微博登录', 
          platform: 'weibo', 
          imageImport: () => import('@/assets/微博.png') 
        }
      ],
      activePlatform: null,            // 当前激活的平台
      activeImage: null,             // 当前显示的图片
      isButtonLayoutHorizontal: false // 按钮是否切换为水平布局
    }
  },
  methods: {
    async handleLogin(item) {
      // 动态导入点击平台的图片
      const imageModule = await item.imageImport(); 
      this.activeImage = imageModule.default; 
      this.activePlatform = item.platform;
      this.isButtonLayoutHorizontal = true; // 按钮切换为水平布局
    },
    goToPasswordLogin() {
      this.$router.push({ name: 'login' });
    },
    onThirdLogin() {
      this.$router.push({ name: 'captchaLogin' });
    },
    onRegister() {
      this.$router.push({ name: 'register' });
    }
  }
}
</script>

<style lang="less" scoped>
// 引入登录页面同款变量
@bg-color: #f9f9f9;
@primary-color: #007bff;
@secondary-color: #f0f0f0;
@text-color: #333;
@link-color: #007bff;
@border-color: #e0e0e0;
@btn-width: 80px; // 折叠按钮宽度

// 复用登录页面的容器、动画等基础样式
.login-container {
  display: flex;
  justify-content: center;
  align-items: center;
  min-height: 100vh;
  background-color: @bg-color;
  animation: fadeIn 1s ease;
}

@keyframes fadeIn {
  from { opacity: 0; }
  to { opacity: 1; }
}

.login-box {
  width: 350px;
  padding: 30px;
  background-color: #fff;
  border-radius: 15px;
  box-shadow: 0 12px 30px rgba(0, 0, 0, 0.12);
  animation: slideIn 1s ease;
}

@keyframes slideIn {
  from { transform: translateY(-30px); opacity: 0; }
  to { transform: translateY(0); opacity: 1; }
}

.login-header {
  text-align: center;
  margin-bottom: 30px;

  h2 {
    font-size: 32px;
    color: @text-color;
    font-weight: 700;
    margin-bottom: 12px;
  }
  p {
    font-size: 16px;
    color: #666;
  }
}

.cell-group {
  border: none;
  border-radius: 12px;
  padding: 24px;
  margin-bottom: 24px;
  box-shadow: 0 4px 16px rgba(0, 0, 0, 0.05);
  background-color: #f9f9f9;
}

// 图片容器样式
.image-wrap {
  text-align: center;
  margin-bottom: 24px;
}

.login-image {
  width: 220px;
  height: auto;
  border-radius: 12px;
  box-shadow: 0 8px 24px rgba(0, 0, 0, 0.12);
}

// 按钮组样式
.btn-group {
  display: flex;
  flex-direction: column;
  gap: 16px;
}

// 水平布局样式
.horizontal-layout {
  flex-direction: row;
  justify-content: center;
  gap: 16px;
}

// 登录按钮基础样式
.login-btn {
  border: none;
  padding: 14px 0;
  font-size: 16px;
  color: #fff;
  border-radius: 24px;
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.15);
  display: flex;
  align-items: center;
  justify-content: center;
  transition: all 0.3s ease;
  min-height: 56px;
}

// 平台独立样式
.login-btn.wechat {
  background-color: #28A745;
}
.login-btn.alipay {
  background-color: #00A3EE;
}
.login-btn.qq {
  background-color: #12B7F5;
}
.login-btn.weibo {
  background-color: #FF4646;
}

// 折叠按钮样式（未选中的按钮）
.collapsed-btn {
  width: @btn-width; // 固定宽度
  padding: 0; // 移除内边距
  
  i {
    margin-right: 0; // 图标右侧无间距
  }
  
  span {
    font-size: 14px; // 文字缩小
  }
}

// 添加其他登录方式按钮样式
  .other-login-options {
    display: flex;
    justify-content: space-between;
    margin-top: 20px;
  }
  
  .other-login-btn {
    border: none;
    background: transparent;
    color: @link-color;
    text-decoration: underline;
    
    &:hover {
      color: darken(@link-color, 10%);
    }
  }
.register-link {
    color: @link-color;
    text-decoration: underline;
    transition: all 0.3s ease;
    border: none; /* 去掉注册链接按钮边框 */

    &:hover {
      color: darken(@link-color, 10%);
    }
  }
.goto {
  text-align: center;
  font-size: 14px;
  color: @text-color;
  margin-top: 20px;

  .register-link {
    color: @link-color;
    text-decoration: underline;
  }
}
</style>